package cn.edu.mju.property.web.controller;

import cn.edu.mju.property.entity.User;
import cn.edu.mju.property.service.UserService;
import cn.edu.mju.property.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/findUser")
public class FindUser extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json; charset=UTF-8");
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");

        String id = request.getParameter("id");
        UserService service = new UserServiceImpl();
        User user = service.findUser(id);
        JSONObject result = new JSONObject();
        result.put("code","0");
        result.put("msg",null);
        result.put("count",1);
        result.put("data", JSON.toJSONString(user));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json; charset=UTF-8");
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");

        String id = request.getParameter("id");
        UserService service = new UserServiceImpl();
        User user = service.findUser(id);
//        JSONObject result = new JSONObject();
//        result.put("code","0");
//        result.put("msg",null);
//        result.put("count",1);
//        result.put("data", JSON.toJSONString(user));
        response.getWriter().write(JSON.toJSONString(user));

    }
}
